import axios from 'axios'

axios.interceptors.request.use(function (config) {
    const token = localStorage.getItem("AUTO_TOKEN")
    if (token && !config.headers.token) {
        config.headers.token = token
    }
    return config
}, function (error) {
    return Promise.reject(error)
})

axios.interceptors.response.use(function (response) {
    return response.data
}, function (error) {
    if (error.response && error.response.status === 401) {
        const msg = error.response.data?.msg || '登录已过期，请重新登录'
        console.error('认证失败:', msg)
    }
    return Promise.reject(error)
})

export default axios
